/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package androidtool.checkconvention;

import androidtool.MainMenu;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Hoang
 */
public class ConventionMainGUI extends javax.swing.JFrame {

    private MainMenu mainMenu;
    private XMLReader xMLReader;
    private JList jList;
    private List<Widget> listWidget;
    private List<CheckListItem> listCheck;
    private String dirFile;
    private String fileName;

    /**
     * Creates new form GettingIdLayout
     */
    public ConventionMainGUI(MainMenu mainMenu) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        }

        this.mainMenu = mainMenu;
        this.dirFile = "";
        this.fileName = "";

        initComponents();

        setVisible(true);
        setLocation(200, 50);

        jPanel1.setLayout(new BorderLayout());
        jPanel1.setDropTarget(new DropTarget() {
            @Override
            public synchronized void drop(DropTargetDropEvent evt) {
                try {
                    evt.acceptDrop(DnDConstants.ACTION_COPY);
                    List<File> droppedFiles = (List<File>) evt
                            .getTransferable().getTransferData(
                            DataFlavor.javaFileListFlavor);
                    if (droppedFiles.size() > 1) {
                        showDialog("Unsupport many file.");
                    } else {
                        File file = droppedFiles.get(0);
                        if (!file.isFile()) {
                            showDialog("Please choose a valid file.");
                        } else {
                            dirFile = file.getAbsolutePath();
                            fileName = file.getName();
                            setXMLReader();
                        }
                    }
                } catch (UnsupportedFlavorException | IOException ex) {
                    showDialog("Unsupport these file.");
                }
            }
        });
    }

    private void setXMLReader() {
        lblFileName.setText(dirFile);
        xMLReader = new XMLReader(dirFile);
        listWidget = xMLReader.getListWidget();

        CheckListItem[] arrCheckListWidget;
        int size = xMLReader.getListWidget().size();
        arrCheckListWidget = new CheckListItem[size];
        for (int i = 0; i < size; i++) {
            arrCheckListWidget[i] = new CheckListItem(listWidget.get(i));
        }

        jList = new JList(arrCheckListWidget);

        // Use a CheckListRenderer (see below) 
        // to renderer list cells
        jList.setCellRenderer(new CheckListRenderer());
        jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        listCheck = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            listCheck.add((CheckListItem) jList.getModel().getElementAt(i));
        }

        // Add a mouse listener to handle changing selection
        jList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                JList list = (JList) event.getSource();

                // Get index of item clicked
                int index = list.locationToIndex(event.getPoint());
                setSelectItem(index, !listCheck.get(index).isSelected());
                revalidate();
                repaint();
            }
        });
        jPanel1.add(new JScrollPane(jList));
        revalidate();
        repaint();
        lblNotice.setText("Import file success!");
    }

    private void setSelectItem(int index, boolean isSelect) {
        listWidget.get(index).setSelected(isSelect);
        listCheck.get(index).setSelected(isSelect);
    }

    private void showDialog(String s) {
        JOptionPane.showMessageDialog(this, s);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        lblFileName = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtResult = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        btnSelectAll = new javax.swing.JButton();
        btnDeselectAll = new javax.swing.JButton();
        lblNotice = new javax.swing.JLabel("<html>a<br/>b</html>");
        jPanel3 = new javax.swing.JPanel();
        btnGenerateVariable = new javax.swing.JButton();
        btnGenerateInitUI = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        btnGetView = new javax.swing.JButton();
        btnGenerateOnClick = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btnCheckConvention = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItemImport = new javax.swing.JMenuItem();
        jMenuItemQuit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Check convention");

        lblFileName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFileName.setText("No file imported.");
        lblFileName.setBorder(javax.swing.BorderFactory.createTitledBorder("File name"));

        txtResult.setColumns(20);
        txtResult.setRows(5);
        txtResult.setBorder(javax.swing.BorderFactory.createTitledBorder("Result"));
        jScrollPane2.setViewportView(txtResult);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("All Element"));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Select"));

        btnSelectAll.setText("Select all");
        btnSelectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectAllActionPerformed(evt);
            }
        });

        btnDeselectAll.setText("Deselect all");
        btnDeselectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeselectAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSelectAll, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDeselectAll, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(btnSelectAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnDeselectAll)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblNotice.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblNotice.setText("No notice!");
        lblNotice.setBorder(javax.swing.BorderFactory.createTitledBorder("Notice"));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Generate"));

        btnGenerateVariable.setText("variable");
        btnGenerateVariable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerateVariableActionPerformed(evt);
            }
        });

        btnGenerateInitUI.setText("initUI()");
        btnGenerateInitUI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerateInitUIActionPerformed(evt);
            }
        });

        jButton4.setText("initControl()");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        btnGetView.setText("getView(...) & ViewHolder");
        btnGetView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGetViewActionPerformed(evt);
            }
        });

        btnGenerateOnClick.setText("onClick(View v)");
        btnGenerateOnClick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerateOnClickActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnGenerateVariable, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnGenerateInitUI, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnGetView, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnGenerateOnClick, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(btnGenerateVariable)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnGenerateInitUI)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnGetView)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnGenerateOnClick)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton1.setText("Copy");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnCheckConvention.setText("Check convention");
        btnCheckConvention.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckConventionActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Open Main menu");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItemImport.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemImport.setText("Import");
        jMenuItemImport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemImportActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemImport);

        jMenuItemQuit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemQuit.setText("Quit");
        jMenuItemQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemQuitActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemQuit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItem1.setText("Help");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItem2.setText("About");
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblFileName, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnCheckConvention, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .addComponent(lblNotice, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblFileName, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblNotice, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(148, 148, 148)
                        .addComponent(btnCheckConvention)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemImportActionPerformed
        chooseFile();
    }//GEN-LAST:event_jMenuItemImportActionPerformed

    private void jMenuItemQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemQuitActionPerformed
        dispose();
    }//GEN-LAST:event_jMenuItemQuitActionPerformed

    private void btnGenerateVariableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateVariableActionPerformed
        txtResult.setText("");
        for (Widget widget : listWidget) {
            if (widget.isSelected()) {
                txtResult.append("private " + widget.getType() + " " + widget.getId() + ";\n");
            }
        }
        lblNotice.setText("Generate variables success!");
    }//GEN-LAST:event_btnGenerateVariableActionPerformed

    private void btnCheckConventionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckConventionActionPerformed
        int nItem = 0;
        int nCorrectItem = 0;

        for (Widget widget : listWidget) {
            if (widget.isSelected()) {
                nItem++;
                if (widget.isCorrected()) {
                    nCorrectItem++;
                }
            }
        }

        if (nItem == 0) {
            lblNotice.setText("You choosed no widget to check");
        } else {
            int rate = (int) ((nCorrectItem * 100.0) / nItem);
            String s = nCorrectItem + "/" + nItem + " widget is corrected (" + rate + "%). \n";

            if (nCorrectItem == nItem) {
                s += "Your code convention is perfect! ^^";
            } else if (rate > 90) {
                s += "Your code convention is good!";
            } else if (rate > 70) {
                s += "Your code convention is bad!";
            } else if (rate > 50) {
                s += "Your code convention is very bad!";
            } else {
                s += "You are stupid!";
            }
            lblNotice.setText(s);
        }
    }//GEN-LAST:event_btnCheckConventionActionPerformed

    private void btnGenerateInitUIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateInitUIActionPerformed
        txtResult.setText("private void initUI() {\n");
        for (Widget widget : listWidget) {
            if (widget.isSelected()) {
                txtResult.append("      " + widget.getId() + " = (" + widget.getType() + ") findViewById(R.id." + widget.getId() + ");\n");
            }
        }
        txtResult.append("}");
        lblNotice.setText("Generate method initUI() success!");
    }//GEN-LAST:event_btnGenerateInitUIActionPerformed

    private void btnSelectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectAllActionPerformed
        int size = listWidget.size();
        for (int i = 0; i < size; i++) {
            setSelectItem(i, true);
        }
        revalidate();
        repaint();
    }//GEN-LAST:event_btnSelectAllActionPerformed

    private void btnDeselectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeselectAllActionPerformed
        int size = listWidget.size();
        for (int i = 0; i < size; i++) {
            setSelectItem(i, false);
        }
        revalidate();
        repaint();
    }//GEN-LAST:event_btnDeselectAllActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        StringSelection sers = new StringSelection(txtResult.getText());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sers, null);
        lblNotice.setText("Copy success.");
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        if (mainMenu == null) {
            mainMenu = new MainMenu();
        }
        mainMenu.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void btnGetViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGetViewActionPerformed
        print("@Override");
        append("public View getView(int position, View convertView, ViewGroup parent) {");
        append("    ViewHolder holder;");
        append("    if (convertView == null) {");
        append("        convertView = layoutInflater.inflate(R.layout." + fileName.substring(0, fileName.length() - 4) + ", null);");
        append("        holder = new ViewHolder();\n");

        for (Widget widget : listWidget) {
            if (widget.isSelected()) {
                append("	holder." + widget.getId() + " = (" + widget.getType() + ") convertView.findViewById(R.id." + widget.getId() + ");");
            }
        }

        append("\n      convertView.setTag(holder);\n");
        append("      } else {");
        append("            holder = (ViewHolder) convertView.getTag();");
        append("      }\n");
        append("      //set data at here\n");
        append("      return convertView;");
        append("}\n");

        append("private class ViewHolder {");
        for (Widget widget : listWidget) {
            if (widget.isSelected()) {
                txtResult.append("	private " + widget.getType() + " " + widget.getId() + ";\n");
            }
        }
        append("}\n");
        lblNotice.setText("Generate class ViewHolder success!");
    }//GEN-LAST:event_btnGetViewActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        txtResult.setText("private void initControl() {\n");
        for (Widget widget : listWidget) {
            if (widget.isSelected()) {
                txtResult.append("      " + widget.getId() + ".setOnClickListener(this);\n");
            }
        }
        txtResult.append("}");
        lblNotice.setText("Generate method initControl() success!");
    }//GEN-LAST:event_jButton4ActionPerformed

    private void btnGenerateOnClickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateOnClickActionPerformed
        print("@Override");
        append("public void onClick(View v) {");
        append("    switch (v.getId()) {");

        int size = listWidget.size();
        for (int i = 0; i < size; i++) {
            Widget widget = listWidget.get(i);
            if (widget.isSelected()) {
                append("          " + "case R.id." + widget.getId() + ":");
                append("          " + widget.getOnClick() + ";");
                if (i == size - 1) {
                    append("          " + "break;");
                } else {
                    append("          " + "break;\n");
                }
            }
        }

        append("    }\n}\n");

        for (Widget widget : listWidget) {
            if (widget.isSelected()) {
                append("private void " + widget.getOnClick() + "{}");
            }
        }
    }//GEN-LAST:event_btnGenerateOnClickActionPerformed

    private void print(String s) {
        txtResult.setText(s + "\n");
    }

    private void append(String s) {
        txtResult.append(s + "\n");
    }
    /*
     * Check id is valid or not
     * 
     * return null if it's valid
     * return the validId if it's not
     */

    private String checkValidId(String widget, String id) {
        String s = id.trim();
        if (widget.contains("Layout")) {
            if (s.contains("layout")) {
                return null;
            } else {
                return ("layout" + s.substring(0, 1).toUpperCase() + s.substring(1));
            }

        } else if (widget.contains("TextV")) {
        }
        return "";
    }

    private void chooseFile() {
        final JFileChooser fc = new JFileChooser();
        JList list = new JList();
        if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
            File file = fc.getSelectedFile();
            dirFile = file.getPath();
            fileName = file.getName();
            setXMLReader();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCheckConvention;
    private javax.swing.JButton btnDeselectAll;
    private javax.swing.JButton btnGenerateInitUI;
    private javax.swing.JButton btnGenerateOnClick;
    private javax.swing.JButton btnGenerateVariable;
    private javax.swing.JButton btnGetView;
    private javax.swing.JButton btnSelectAll;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItemImport;
    private javax.swing.JMenuItem jMenuItemQuit;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblFileName;
    private javax.swing.JLabel lblNotice;
    private javax.swing.JTextArea txtResult;
    // End of variables declaration//GEN-END:variables
}
